package cn.com.fuhuitong.main.order.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.mine.entity.AddressDefaultResponse
import cn.com.fuhuitong.main.mine.entity.AddressEntity
import cn.com.fuhuitong.main.order.entity.*
import cn.com.fuhuitong.utils.SpanUtils
import com.alibaba.android.vlayout.LayoutHelper
import com.bumptech.glide.Glide
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import kotlinx.android.synthetic.main.item_order_submit_address.view.*
import kotlinx.android.synthetic.main.item_order_submit_bottom.view.*
import kotlinx.android.synthetic.main.item_order_submit_goods.view.*
import kotlinx.android.synthetic.main.item_order_submit_integral.view.*
import kotlinx.android.synthetic.main.item_order_submit_nature.view.*
import kotlinx.android.synthetic.main.item_order_submit_remark.view.*

enum class SVT(value: Int) {
    SUBMIT_ADDRESS(1),
    SUBMIT_GOODS(2),
    SUBMIT_INTEGRAL(3),
    SUBMIT_NATURE(4),
    SUBMIT_REMARK(5),
    SUBMIT_BOTTOM(5)
}

class OrderSubmitAddressAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<AddressEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_submit_address,
    mItemViewType = SVT.SUBMIT_ADDRESS.ordinal
) {
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(itemView: View, position: Int, itemData: AddressEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        if (itemData.id == 0) {
            itemView.text_order_submit_address_details.visibility = View.GONE
            itemView.text_order_submit_address_name.text = "请选择地址"
        } else {
            itemView.text_order_submit_address_details.visibility = View.VISIBLE

            itemView.text_order_submit_address_name.text =
                "${itemData.contact_name}  ${itemData.contact_mobile}"
            itemView.text_order_submit_address_details.text =
                "${itemData.province}${itemData.city}${itemData.area}${itemData.address}"
            itemView.text_order_submit_address_is_default.visibility =
                if (itemData.is_default == 1) View.VISIBLE else View.GONE
        }
    }
}

class OrderSubmitGoodsAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<Good>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_submit_goods,
    mItemViewType = SVT.SUBMIT_GOODS.ordinal
) {
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(itemView: View, position: Int, itemData: Good) {
        super.onBindViewHolder(itemView, position, itemData)
        Glide.with(itemView).load(getImageUrl(itemData.goods.image))
            .into(itemView.image_order_submit_icon)
        itemView.text_order_submit_name.text = itemData.goods.title
        itemView.text_order_submit_specs.visibility =
            if (itemData.goods.is_specifications == 1) View.VISIBLE else View.GONE
        itemView.text_order_submit_specs.text =
            if (itemData.specifications != null) "规格：${itemData.specifications.specifications_name}" else ""
        itemView.text_order_submit_price.text =
            SpanUtils.setMoneyStyle(
                if (itemData.goods.is_specifications == 1 && itemData.specifications != null)
                    itemData.specifications.specifications_price.toDouble() else itemData.goods.price.toDouble()
            )
        itemView.text_order_submit_num.text = "x${itemData.num}"
    }
}

class OrderSubmitIntegralAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<SubmitIntegral>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_submit_integral,
    mItemViewType = SVT.SUBMIT_INTEGRAL.ordinal
) {
    init {
        addChildClickViewIds(
            R.id.fl_order_submit_shop_details,
            R.id.cl_order_submit_shop,
            R.id.check_order_submit_switch
        )
    }

    override fun onBindViewHolder(itemView: View, position: Int, itemData: SubmitIntegral) {
        super.onBindViewHolder(itemView, position, itemData)
        if (itemData.flagship_store_id == 1) {
            itemView.cl_order_submit_shop.visibility = View.VISIBLE
        } else {
            itemView.cl_order_submit_shop.visibility = View.GONE
        }
        if (itemData.flagship_store_name.isEmpty()) {
            itemView.text_order_submit_shop.text = "门店选择"
            itemView.text_order_submit_shop_details.visibility = View.VISIBLE
        } else {
            itemView.text_order_submit_shop.text = itemData.flagship_store_name
            itemView.text_order_submit_shop_details.visibility = View.GONE
        }
    }
}

data class SubmitIntegral(
    val flagship_store_id: Int = 0,
    val flagship_store_name: String = ""
)

class OrderSubmitNatureAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<SubmitNatureEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_submit_nature,
    mItemViewType = SVT.SUBMIT_NATURE.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: SubmitNatureEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_submit_total.text = SpanUtils.setMoneyStyle(itemData.total.toDouble())
        itemView.text_order_submit_deduction.text =
            SpanUtils.setMoneyStyle(itemData.price)
        itemView.text_order_submit_freight.text = SpanUtils.setMoneyStyle(0.toDouble())
    }
}

class OrderSubmitRemarkAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_submit_remark,
    mItemViewType = SVT.SUBMIT_REMARK.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: String) {
        super.onBindViewHolder(itemView, position, itemData)
        RxTextView.textChanges(itemView.text_order_submit_remark)
            .subscribe {
                setOnItemChildClick(itemView.text_order_submit_remark, position, it.toString())
            }
    }
}

class OrderSubmitBottomAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_submit_bottom,
    mItemViewType = SVT.SUBMIT_REMARK.ordinal
) {
    init {
        addChildClickViewIds(R.id.text_order_submit_comfit)
    }

    override fun onBindViewHolder(itemView: View, position: Int, itemData: String) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_submit_total_price.text = "￥${itemData}"
    }
}